package com.waimai.mobile.ui.fragment;

import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.waimai.mobile.R;
import com.waimai.mobile.api.WaiMaiServer;
import com.waimai.mobile.api.entity.Restaurant;
import com.waimai.mobile.api.model.ResponseModel;
import com.waimai.mobile.ui.activity.LocationActivity;
import com.waimai.mobile.ui.activity.MainActivity;
import com.waimai.mobile.ui.activity.RestaurantActivity;
import com.waimai.mobile.ui.activity.RestaurantActivity_;
import com.waimai.mobile.ui.adapter.RestaurantItemAdpater;
import com.waimai.mobile.ui.listener.RecyclerItemClickListener;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.RestService;

import java.util.List;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * Created by lin on 2015/5/29.
 */

@EFragment(R.layout.fragment_home)
public class HomeFragment extends Fragment {

    public List<Restaurant> restaurants;

    @RestService
    public WaiMaiServer server;

    @ViewById
    public EditText keyWordsEdt;

    @ViewById
    public Button searchBtn;

    @ViewById
    public TextView mentionMsgTv;

    @ViewById
    public PtrClassicFrameLayout containerPtr;

    @ViewById
    public RecyclerView restRv;

    @Bean
    public RestaurantItemAdpater adapter;

    @AfterInject
    public void afterInject(){
        server.setHeader("Content-Type", "application/json");
    }

    @AfterViews
    public void afterViews() {
        restRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        new RecyclerItemClickListener(getActivity(), restRv, new RecyclerItemClickListener.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(getActivity(), RestaurantActivity_.class);
                intent.putExtra("restaurantId", restaurants.get(position).getRestaurantId());
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });

        containerPtr.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout ptrFrameLayout) {
                loadRestaurants("");
            }
        });

        loading();
        loadRestaurants("");
    }


    @Click(R.id.searchBtn)
    public void searchBtnClick(){
        String keyWords = keyWordsEdt.getText().toString().trim();
        if(keyWords == null|| "".equals(keyWords)){
            Toast.makeText(getActivity(), "搜索词为空", Toast.LENGTH_SHORT).show();
            return ;
        }

        loading();
        loadRestaurants(keyWords);
    }

    @Background
    public void loadRestaurants(String keyWords){
        ResponseModel<List<Restaurant>> model;
        try{
            model = server.restaurants(keyWords);
            if(model != null){
                switch (model.getStatus().getStatusCode()) {
                    case ResponseModel.STATUS_GET_DATA_SUCCESS:
                        loadedSuccess(model.getData());
                        break;
                    case ResponseModel.STATUS_GET_DATA_NONE:
                        loadNoneData();
                        break;
                    case ResponseModel.STATUS_SERVER_ERROR:
                        serverError();
                        break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            netError();
        }
    }

    @UiThread
    public void loadedSuccess(List<Restaurant> restaurants){
        containerPtr.setVisibility(View.VISIBLE);
        mentionMsgTv.setVisibility(View.GONE);

        this.restaurants = restaurants;
        adapter.setRestaurants(restaurants);
        restRv.setAdapter(adapter);
        containerPtr.refreshComplete();
    }


    @UiThread
    public void loading(){
        containerPtr.setVisibility(View.GONE);
        mentionMsgTv.setText("正在加载...");
        mentionMsgTv.setVisibility(View.VISIBLE);
        mentionMsgTv.setClickable(false);
    }

    @UiThread
    public void loadNoneData(){
        containerPtr.setVisibility(View.GONE);
        mentionMsgTv.setText("附近没有餐厅哦，点击刷新试试");
        mentionMsgTv.setVisibility(View.VISIBLE);
        mentionMsgTv.setClickable(true);
        containerPtr.refreshComplete();
    }

    @UiThread
    public void serverError() {
        containerPtr.setVisibility(View.GONE);
        mentionMsgTv.setText("服务器异常，点击重试");
        mentionMsgTv.setVisibility(View.VISIBLE);
        mentionMsgTv.setClickable(true);
        containerPtr.refreshComplete();
    }

    @UiThread
    public void netError() {
        containerPtr.setVisibility(View.GONE);
        mentionMsgTv.setText("网络错误，点击重试");
        mentionMsgTv.setVisibility(View.VISIBLE);
        mentionMsgTv.setClickable(true);
        containerPtr.refreshComplete();
    }

    @Click(R.id.mentionMsgTv)
    public void mentionMsgTvClick(View v){
        loading();
        loadRestaurants("");
    }
}